# SleepMirror - A reflection of your sleep quality
# 
# sm_edge_detect.rb
# This is a specific class to handle an "edge detection" for images that merge or blend
# colors.  TODO: abstract this behavior into a parent class.
# 
# 
# ©2010 JohnGary, LLC.  All Rights Reserved.
# 
# 
# Changelog:
# 
# 
# 

require 'sm_panel'
require "rubygame"

include Rubygame
include Rubygame::Events
include Rubygame::EventActions
include Rubygame::EventTriggers

DEFAULT_ASSET = "SM_bessy.jpg"

class SM_edge_detect < SM_panel
  attr_accessor :screen, :image

  # A new SM_edge_detect adds a Rubygame::Screen to the base SM_panel class.
  # It is intended to display an image representing a test blend, and capture the
  # mouse clicks of the user, indicating where a color "stops" or fades out.
  def initialize(name="", instructions="", asset=DEFAULT_ASSET)
    super(name, instructions, asset)
    @screen=nil
    @answer=[] #override the instance variable to be an array instead of a string
    # Load the image
    this_dir = File.dirname( __FILE__ )
    file = File.expand_path( @asset, this_dir ) # default file
    @image = Surface.load( file ) #In this context, the asset is expected to be an image
    @image.colorkey=@image.get_at([0,0])
    @image.alpha=255


    left_click_hook = EventHook.new(:owner=>self,
       :trigger => MouseReleaseTrigger.new(:mouse_left),
       :action  => MethodAction.new(:splat))

    append_hook(left_click_hook) #Add the left click hooks to the event table
    #@begin_rect = Rect.new([296,173,252,51])
    #@end_rect   = Rect.new([296,240,252,51]) #the region signalling "I'm Done."

  end

  def splat (event)
    #s = Surface.new([20,20])
    #s.fill(:white)
    #s.blit(@background,event.pos)
    TTF.setup()
    ttfont_path = File.join(File.dirname(__FILE__),"FreeSans.ttf")
    ttfont = TTF.new( ttfont_path, 10 )
#
    result = ttfont.render( "^"+event.pos.to_s+", RGBa="+@image.get_at(*event.pos).to_s, true, :red )
    result.blit( @screen, event.pos )
    capture_answer(event)
#    puts "Splat is in Begin" if @begin_rect.collide_point?(*event.pos)
#    puts "Splat is in End" if @end_rect.collide_point?(*event.pos)
#    if @begin_rect.collide_point?(*event.pos)
#      @answer = "start"
#      exit
#    end
#    if @end_rect.collide_point?(*event.pos)
#      @answer = "end"
#      exit
#    end
  end

  def capture_answer(event)
      puts "Captured #{event.pos}"
      @answer << event.pos
  end
  def exit_script
    puts @answer.to_s
    exit
  end

  def perform
    # Setup the Screen for this test
    @screen = Screen.new(@image.size,0,[HWSURFACE])
    @screen.title = "#{@name} - #{@asset}" #File.basename(file)
    @screen.show_cursor=TRUE
    #@screen.fill(@image.colorkey) #Fill the screen with the image's background
    @image.blit( @screen, [0,0] ) #Paste the asset onto the screen
    # Show the instructions
    TTF.setup()
    ttfont_path = File.join(File.dirname(__FILE__),"FreeSans.ttf")
    ttfont = TTF.new( ttfont_path, 20 )
    result = ttfont.render( "Click on the outermost blue line.  Press ESC to quit.",true,:gray)
    result.blit( @screen,[@image.size[0]/2-result.size[0]/2,5])

    # Setup the event queue for this test
    events=EventQueue.new
    events.enable_new_style_events
    # Loop until we get an answer or an exit.
    loop do
      events = Rubygame::Events.fetch_sdl_events
      events.each do |event|
        #@handler.handle( event )
        handle( event )
      end
      @screen.update
      sleep 0.1
    end
  end

end
